package org.ielts.service;

import java.util.Date;
import java.util.Set;

import org.ielts.domain.LearnStatus;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.FinishCourseLog;
import org.ielts.domain.log.FreezeCourseLog;
import org.ielts.domain.log.ResumeCourseLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.security.User;
import org.ielts.repository.FinishCourseLogRepository;
import org.ielts.repository.FreezeCourseLogRepository;
import org.ielts.repository.ResumeCourseLogRepository;
import org.ielts.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yifeng.ProfileContext;
import com.yifeng.profile.Profile;

@Service
public class SchoolService {

	@Autowired
	FinishCourseLogRepository finishCourseLogRepository;

	@Autowired
	ResumeCourseLogRepository resumeCourseLogRepository;

	@Autowired
	FreezeCourseLogRepository freezeCourseLogRepository;

	@Autowired
	StudentRepository studentRepository;

	@Transactional
	public FinishCourseLog finishCourse(String studentId, String signId, String title) {
		FinishCourseLog finishCourseLog = new FinishCourseLog();

		Student student = studentRepository.findOne(studentId);
		Set<SignCourse> signCourses = student.getCourses();
		for (SignCourse signCourseLog : signCourses) {
			if (signCourseLog.getId().equals(signId)) {
				signCourseLog.setLearnStatus(LearnStatus.Finished);
				// 实际结课时间为当前时间
				signCourseLog.setRealEndTime(new Date());
				finishCourseLog.setSignCourse(signCourseLog);
			}
		}

		User creator = (User) Profile.getInstance().getUser();
		finishCourseLog.setCreator(creator);
		finishCourseLog.setTitle(title);
		finishCourseLog.setStudent(student);

		finishCourseLogRepository.save(finishCourseLog);

		return finishCourseLog;
	}

	@Transactional
	public ResumeCourseLog resumeCourse(String studentId, String signId, String title) {
		ResumeCourseLog resumeCourseLog = new ResumeCourseLog();

		Student student = studentRepository.findOne(studentId);
		Set<SignCourse> signCourses = student.getCourses();
		for (SignCourse signCourseLog : signCourses) {
			if (signCourseLog.getId().equals(signId)) {
				signCourseLog.setLearnStatus(LearnStatus.Learning);
				resumeCourseLog.setSignCourse(signCourseLog);
			}
		}

		User creator = (User) Profile.getInstance().getUser();
		resumeCourseLog.setCreator(creator);
		resumeCourseLog.setTitle(title);
		resumeCourseLog.setStudent(student);

		resumeCourseLogRepository.save(resumeCourseLog);

		return resumeCourseLog;
	}

	@Transactional
	public FreezeCourseLog freezCourse(String studentId, String signId, Date resumeDate, Date endDate, String title) {
		FreezeCourseLog freezeCourseLog = new FreezeCourseLog();

		Student student = studentRepository.findOne(studentId);
		Set<SignCourse> signCourses = student.getCourses();
		for (SignCourse signCourseLog : signCourses) {
			if (signCourseLog.getId().equals(signId)) {
				signCourseLog.setLearnStatus(LearnStatus.Freeze);
				signCourseLog.setEndTime(endDate);
				freezeCourseLog.setSignCourse(signCourseLog);
			}
		}

		User creator = (User) Profile.getInstance().getUser();
		freezeCourseLog.setCreator(creator);
		freezeCourseLog.setTitle(title);
		freezeCourseLog.setStudent(student);
		freezeCourseLog.setEndDate(endDate);
		freezeCourseLog.setResumeDate(resumeDate);

		freezeCourseLogRepository.save(freezeCourseLog);

		return freezeCourseLog;
	}
}
